####MEAN LEVEL MODELS#####
#simple models while controlling for age, period, and cohort
anes_meanPolviews <- estimatr::lm_robust(att ~ genPew + year + ageGroup, se_type = "stata", cluster = anes_Polviews$year, anes_Polviews)
anes_meansWelfare <- estimatr::lm_robust(att ~ genPew + year + ageGroup, se_type = "stata", cluster = anes_Swelfare$year, anes_Swelfare)
anes_meanrGender  <- estimatr::lm_robust(att ~ genPew + year + ageGroup, se_type = "stata", cluster = anes_Racgender$year, anes_Racgender)
anes_meancMoral   <- estimatr::lm_robust(att ~ genPew + year + ageGroup, se_type = "stata", cluster = anes_Cultmoral$year, anes_Cultmoral)
anes_meanBlkaid   <- estimatr::lm_robust(att ~ genPew + year + ageGroup, se_type = "stata", cluster = anes_Blkaid$year, anes_Blkaid)

#controlled models with controls for white, protestants, and education
anes_meanPolviews_C <- estimatr::lm_robust(att ~ genPew + year + ageGroup + college + white + protcattend, se_type = "stata", cluster = anes_Polviews$year, anes_Polviews)
anes_meansWelfare_C <- estimatr::lm_robust(att ~ genPew + year + ageGroup + college + white + protcattend, se_type = "stata", cluster = anes_Swelfare$year, anes_Swelfare)
anes_meanrGender_C  <- estimatr::lm_robust(att ~ genPew + year + ageGroup + college + white + protcattend, se_type = "stata", cluster = anes_Racgender$year, anes_Racgender)
anes_meancMoral_C   <- estimatr::lm_robust(att ~ genPew + year + ageGroup + college + white + protcattend, se_type = "stata", cluster = anes_Cultmoral$year, anes_Cultmoral)
anes_meanBlkaid_C   <- estimatr::lm_robust(att ~ genPew + year + ageGroup + college + white + protcattend, se_type = "stata", cluster = anes_Blkaid$year, anes_Blkaid)

#run the same models with party id as the dependent variable 
anes_meanPolviews_pid <- estimatr::lm_robust(partyid ~ genPew + year + ageGroup, se_type = "stata", cluster = anes_Polviews$year, anes_Polviews)
anes_meansWelfare_pid <- estimatr::lm_robust(partyid ~ genPew + year + ageGroup, se_type = "stata", cluster = anes_Swelfare$year, anes_Swelfare)
anes_meanrGender_pid  <- estimatr::lm_robust(partyid ~ genPew + year + ageGroup, se_type = "stata", cluster = anes_Racgender$year, anes_Racgender)
anes_meancMoral_pid   <- estimatr::lm_robust(partyid ~ genPew + year + ageGroup, se_type = "stata", cluster = anes_Cultmoral$year, anes_Cultmoral)
anes_meanBlkaid_pid   <- estimatr::lm_robust(partyid ~ genPew + year + ageGroup, se_type = "stata", cluster = anes_Blkaid$year, anes_Blkaid)

#run the same models with party id as the dependent variable 
anes_meanPolviews_pid_C <- estimatr::lm_robust(partyid ~ genPew + year + ageGroup + college + white + protcattend, se_type = "stata", cluster = anes_Polviews$year, anes_Polviews)
anes_meansWelfare_pid_C <- estimatr::lm_robust(partyid ~ genPew + year + ageGroup + college + white + protcattend, se_type = "stata", cluster = anes_Swelfare$year, anes_Swelfare)
anes_meanrGender_pid_C  <- estimatr::lm_robust(partyid ~ genPew + year + ageGroup + college + white + protcattend, se_type = "stata", cluster = anes_Racgender$year, anes_Racgender)
anes_meancMoral_pid_C   <- estimatr::lm_robust(partyid ~ genPew + year + ageGroup + college + white + protcattend, se_type = "stata", cluster = anes_Cultmoral$year, anes_Cultmoral)
anes_meanBlkaid_pid_C   <- estimatr::lm_robust(partyid ~ genPew + year + ageGroup + college + white + protcattend, se_type = "stata", cluster = anes_Blkaid$year, anes_Blkaid)

####DIVERGENCE MODELS#####
#first, obtain the absolute residuals from the previous models
anes_Polviews$div  <- abs(anes_meanPolviews$fitted.values - anes_Polviews$att)
anes_Swelfare$div  <- abs(anes_meansWelfare$fitted.values - anes_Swelfare$att)
anes_Racgender$div <- abs(anes_meanrGender$fitted.values - anes_Racgender$att)
anes_Cultmoral$div <- abs(anes_meancMoral$fitted.values - anes_Cultmoral$att)
anes_Blkaid$div    <- abs(anes_meanBlkaid$fitted.values - anes_Blkaid$att)

#repeat for party id
anes_Polviews$div_pid  <- abs(anes_meanPolviews_pid$fitted.values - anes_Polviews$partyid)
anes_Swelfare$div_pid  <- abs(anes_meansWelfare_pid$fitted.values - anes_Swelfare$partyid)
anes_Racgender$div_pid <- abs(anes_meanrGender_pid$fitted.values - anes_Racgender$partyid)
anes_Cultmoral$div_pid <- abs(anes_meancMoral_pid$fitted.values - anes_Cultmoral$partyid)
anes_Blkaid$div_pid    <- abs(anes_meanBlkaid_pid$fitted.values - anes_Blkaid$partyid)

#same procedure for the models with demographic controls
anes_Polviews$div_C  <- abs(anes_meanPolviews_C$fitted.values - anes_Polviews$att)
anes_Swelfare$div_C  <- abs(anes_meansWelfare_C$fitted.values - anes_Swelfare$att)
anes_Racgender$div_C <- abs(anes_meanrGender_C$fitted.values - anes_Racgender$att)
anes_Cultmoral$div_C <- abs(anes_meancMoral_C$fitted.values - anes_Cultmoral$att)
anes_Blkaid$div_C    <- abs(anes_meanBlkaid_C$fitted.values - anes_Blkaid$att)

#repeat for party id
anes_Polviews$div_pid_C  <- abs(anes_meanPolviews_pid_C$fitted.values - anes_Polviews$partyid)
anes_Swelfare$div_pid_C  <- abs(anes_meansWelfare_pid_C$fitted.values - anes_Swelfare$partyid)
anes_Racgender$div_pid_C <- abs(anes_meanrGender_pid_C$fitted.values - anes_Racgender$partyid)
anes_Cultmoral$div_pid_C <- abs(anes_meancMoral_pid_C$fitted.values - anes_Cultmoral$partyid)
anes_Blkaid$div_pid_C    <- abs(anes_meanBlkaid_pid_C$fitted.values - anes_Blkaid$partyid)

#next, run the lm models using the absolute residuals as the output variables
anes_divPolviews <- estimatr::lm_robust(div ~ genPew + year + ageGroup, se_type = "stata", cluster = anes_Polviews$year, anes_Polviews)
anes_divsWelfare <- estimatr::lm_robust(div ~ genPew + year + ageGroup, se_type = "stata", cluster = anes_Swelfare$year, anes_Swelfare)
anes_divrGender  <- estimatr::lm_robust(div ~ genPew + year + ageGroup, se_type = "stata", cluster = anes_Racgender$year, anes_Racgender)
anes_divcMoral   <- estimatr::lm_robust(div ~ genPew + year + ageGroup, se_type = "stata", cluster = anes_Cultmoral$year, anes_Cultmoral)
anes_divBlkaid   <- estimatr::lm_robust(div ~ genPew + year + ageGroup,  se_type = "stata", cluster = anes_Blkaid$year,anes_Blkaid)

#again, same procedure for the demographic models
anes_divPolviews_C <- estimatr::lm_robust(div_C ~ genPew + year + ageGroup + college + white + protcattend, se_type = "stata", cluster = anes_Polviews$year, anes_Polviews)
anes_divsWelfare_C <- estimatr::lm_robust(div_C ~ genPew + year + ageGroup + college + white + protcattend, se_type = "stata", cluster = anes_Swelfare$year, anes_Swelfare)
anes_divrGender_C  <- estimatr::lm_robust(div_C ~ genPew + year + ageGroup + college + white + protcattend, se_type = "stata", cluster = anes_Racgender$year, anes_Racgender)
anes_divcMoral_C   <- estimatr::lm_robust(div_C ~ genPew + year + ageGroup + college + white + protcattend, se_type = "stata", cluster = anes_Cultmoral$year, anes_Cultmoral)
anes_divBlkaid_C   <- estimatr::lm_robust(div_C ~ genPew + year + ageGroup + college + white + protcattend, se_type = "stata", cluster = anes_Blkaid$year, anes_Blkaid)

#repeat for partyid
anes_divPolviews_pid <- estimatr::lm_robust(div_pid ~ genPew + year + ageGroup, se_type = "stata", cluster = anes_Polviews$year, anes_Polviews)
anes_divsWelfare_pid <- estimatr::lm_robust(div_pid ~ genPew + year + ageGroup, se_type = "stata", cluster = anes_Swelfare$year, anes_Swelfare)
anes_divrGender_pid  <- estimatr::lm_robust(div_pid ~ genPew + year + ageGroup, se_type = "stata", cluster = anes_Racgender$year, anes_Racgender)
anes_divcMoral_pid   <- estimatr::lm_robust(div_pid ~ genPew + year + ageGroup, se_type = "stata", cluster = anes_Cultmoral$year, anes_Cultmoral)
anes_divBlkaid_pid   <- estimatr::lm_robust(div_pid ~ genPew + year + ageGroup, se_type = "stata", cluster = anes_Blkaid$year, anes_Blkaid)

#repeat#2
anes_divPolviews_pid_C <- estimatr::lm_robust(div_pid_C ~ genPew + year + ageGroup + college + white + protcattend, se_type = "stata", cluster = anes_Polviews$year, anes_Polviews)
anes_divsWelfare_pid_C <- estimatr::lm_robust(div_pid_C ~ genPew + year + ageGroup + college + white + protcattend, se_type = "stata", cluster = anes_Swelfare$year,  anes_Swelfare)
anes_divrGender_pid_C  <- estimatr::lm_robust(div_pid_C ~ genPew + year + ageGroup + college + white + protcattend, se_type = "stata", cluster = anes_Racgender$year,  anes_Racgender)
anes_divcMoral_pid_C   <- estimatr::lm_robust(div_pid_C ~ genPew + year + ageGroup + college + white + protcattend, se_type = "stata", cluster = anes_Cultmoral$year,  anes_Cultmoral)
anes_divBlkaid_pid_C   <- estimatr::lm_robust(div_pid_C ~ genPew + year + ageGroup + college + white + protcattend, se_type = "stata", cluster = anes_Blkaid$year,  anes_Blkaid)

#compute variables in main data frame
anes_Polviews <- anes_Polviews %>%
  mutate(sort_dv = (polviews - anes_meanPolviews$fitted.values) / anes_divPolviews$fitted.values,
         sort_iv = (partyid - anes_meanPolviews_pid$fitted.values) / anes_divPolviews_pid$fitted.values,
         sort_dv_C = (polviews - anes_meanPolviews_C$fitted.values) / anes_divPolviews_C$fitted.values,
         sort_iv_C = (partyid - anes_meanPolviews_pid_C$fitted.values) / anes_divPolviews_pid_C$fitted.values)

anes_Swelfare <- anes_Swelfare %>%
  mutate(sort_dv = (att - anes_meansWelfare$fitted.values) / anes_divsWelfare$fitted.values,
         sort_iv = (partyid - anes_meansWelfare_pid$fitted.values) / anes_divsWelfare_pid$fitted.values,
         sort_dv_C = (att - anes_meansWelfare_C$fitted.values) / anes_divsWelfare_C$fitted.values,
         sort_iv_C = (partyid - anes_meansWelfare_pid_C$fitted.values) / anes_divsWelfare_pid_C$fitted.values)

anes_Racgender <- anes_Racgender %>%
  mutate(sort_dv = (att -  anes_meanrGender$fitted.values) / anes_divrGender$fitted.values,
         sort_iv = (partyid - anes_meanrGender_pid$fitted.values) / anes_divrGender_pid$fitted.values,
         sort_dv_C = (att -  anes_meanrGender_C$fitted.values) / anes_divrGender_C$fitted.values,
         sort_iv_C = (partyid - anes_meanrGender_pid_C$fitted.values) / anes_divrGender_pid_C$fitted.values)

anes_Cultmoral <- anes_Cultmoral %>%
  mutate(sort_dv = (att -  anes_meancMoral$fitted.values) / anes_divcMoral$fitted.values,
         sort_iv = (partyid - anes_meancMoral_pid$fitted.values) / anes_divcMoral_pid$fitted.values,
         sort_dv_C = (att -  anes_meancMoral_C$fitted.values) / anes_divcMoral_C$fitted.values,
         sort_iv_C = (partyid - anes_meancMoral_pid_C$fitted.values) / anes_divcMoral_pid_C$fitted.values)

anes_Blkaid <- anes_Blkaid %>%
  mutate(sort_dv = (att -  anes_meanBlkaid$fitted.values) / anes_divBlkaid$fitted.values,
         sort_iv = (partyid - anes_meanBlkaid_pid$fitted.values) / anes_divBlkaid_pid$fitted.values,
         sort_dv_C = (att -  anes_meanBlkaid_C$fitted.values) / anes_divBlkaid_C$fitted.values,
         sort_iv_C = (partyid - anes_meanBlkaid_pid_C$fitted.values) / anes_divBlkaid_pid_C$fitted.values)

####SORTING MODELS#####
sort_anesPolviews <- estimatr::lm_robust(sort_dv ~ sort_iv * genPew + year * sort_iv + ageGroup * sort_iv, se_type = "stata", cluster = anes_Polviews$year, anes_Polviews)

#social welfare
sort_anesSwelfare <- estimatr::lm_robust(sort_dv ~ sort_iv * genPew + year * sort_iv + ageGroup * sort_iv, se_type = "stata", cluster = anes_Swelfare$year, anes_Swelfare)

#Race & Gender
sort_anesRacgender <- estimatr::lm_robust(sort_dv ~ sort_iv * genPew + year * sort_iv + ageGroup * sort_iv, se_type = "stata", cluster = anes_Racgender$year,anes_Racgender)

#culture and morality
sort_anesCultmoral <- estimatr::lm_robust(sort_dv ~ sort_iv * genPew + year * sort_iv + ageGroup * sort_iv, se_type = "stata", cluster = anes_Cultmoral$year,anes_Cultmoral)

# aid to blacks
sort_anesBlkaid <- estimatr::lm_robust(sort_dv ~ sort_iv * genPew + year * sort_iv + ageGroup * sort_iv, se_type = "stata", cluster = anes_Blkaid$year, anes_Blkaid)

####SORTING MODELS WITH DEMOGRAPHIC CONTROLS#####
sort_C_anesPolviews <- estimatr::lm_robust(sort_dv_C ~ sort_iv_C * genPew + year * sort_iv_C + ageGroup * sort_iv_C +
                             college * sort_iv_C + white * sort_iv_C + protcattend * sort_iv_C, se_type = "stata", cluster = anes_Polviews$year, anes_Polviews)

#social welfare
sort_C_anesSwelfare <- estimatr::lm_robust(sort_dv_C ~ sort_iv_C * genPew + year * sort_iv_C + ageGroup * sort_iv_C +
                             college * sort_iv_C + white * sort_iv_C + protcattend * sort_iv_C, se_type = "stata", cluster = anes_Swelfare$year, anes_Swelfare)

#Race & Gender
sort_C_anesRacgender <- estimatr::lm_robust(sort_dv_C ~ sort_iv_C * genPew + year * sort_iv_C + ageGroup * sort_iv_C +
                              college * sort_iv_C + white * sort_iv_C + protcattend * sort_iv_C, se_type = "stata", cluster = anes_Racgender$year, anes_Racgender)

#culture and morality
sort_C_anesCultmoral <- estimatr::lm_robust(sort_dv_C ~ sort_iv_C * genPew + year * sort_iv_C + ageGroup * sort_iv_C +
                              college * sort_iv_C + white * sort_iv_C + protcattend * sort_iv_C, se_type = "stata", cluster = anes_Cultmoral$year, anes_Cultmoral)
#aid to blacks
sort_C_anesBlkaid <- estimatr::lm_robust(sort_dv_C ~ sort_iv_C * genPew + year * sort_iv_C + ageGroup * sort_iv_C +
                           college * sort_iv_C + white * sort_iv_C + protcattend * sort_iv_C, se_type = "stata", cluster = anes_Blkaid$year, anes_Blkaid)

